package general.zjitc.net.androidjgeneral2;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private TabLayout mTabLayout;
    private ViewPager mVp;
    private List<String> mTitles;
    private List<Fragment> mFragments;
    private List<Integer> mContent;
    /**
     * 1.定义主布局activity_main.xml
     * 2.创建menu包，main.xml
     * 3.创建每个Activity相对应的Fragment，fragment.xml
     * 4.创建recyclerView所对应的recycler.xml
     * 5.创建RecyclerView的适配器
     * 6.创建TabLayout的适配器
     * 7.创建每个fragment.xml所对应的ListFragment
     * 8.定义MainActivity
     * @param savedInstanceState
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initViews();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    private void initViews() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.search:
                        break;
                    case R.id.shop:
                        break;
                    case R.id.about:
                        break;
                        default:break;
                }
                return true;
            }
        });
        mTitles.add("壁纸推荐");
        mTitles.add("电脑壁纸");
        mTitles.add("手机壁纸");
        for (int i = 0; i <mTitles.size() ; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(mTitles.get(i)));
        }

//        mContent.add("壁纸");
//        mContent.add("壁纸");
//        mContent.add("壁纸");
//        mContent.add("壁纸");
//        mContent.add("壁纸");
//        mContent.add("壁纸");
//        mContent.add("壁纸");
//        mContent.add("壁纸");
//        mContent.add("壁纸");
//        mContent.add("壁纸");
        mContent.add(R.drawable.dm);
        mContent.add(R.drawable.dm2);
        ListFragment listFragment=new ListFragment(mContent);
        ListFragment2 listFragment2=new ListFragment2();
        ListFragment3 listFragment3=new ListFragment3();
        mFragments.add(listFragment);
        mFragments.add(listFragment2);
        mFragments.add(listFragment3);

        FragmentPagerAdapter fragmentPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager(),mTitles,mFragments);
        mTabLayout.setupWithViewPager(mVp);
        mVp.setAdapter(fragmentPagerAdapter);
    }

    private void init() {
        mToolbar=findViewById(R.id.tool);
        mTabLayout=findViewById(R.id.tab);
        mVp=findViewById(R.id.vp);
        mTitles=new ArrayList<>();
        mFragments=new ArrayList<>();
        mContent=new ArrayList<Integer>();
    }
}
